package com.mgetool.ui.util;
/**
 * 时间工具类，根据给出的时间，换算出多少年、月、天、小时、分钟、秒。
 * 时间单位为毫秒<br>
 * 比如 65000毫秒，为1分零5秒
 * @author wangbin
 *
 */
public class ICalender {

	public static final byte YEAR = 0x1;
	public static final byte MONTH = 0x2;
	public static final byte DAY = 0x4;
	public static final byte HOUR = 0x8;
	public static final byte MINUTE = 0x10;
	public static final byte SECOND = 0x20;
	
	private long timeMillis;
	
	public static final int UNIT_YEAR = 365 * 24 * 60 * 60 * 1000;
	public static final int UNIT_MONTH = 30 * 24 * 60 * 60 * 1000;
	public static final int UNIT_DAY = 24 * 60 * 60 * 1000;
	public static final int UNIT_HOUR = 60 * 60 * 1000;
	public static final int UNIT_MINUTE = 60 * 1000;
	public static final int UNIT_SECOND = 1000;
	//最大时间单位
	private int maxTimeUnit;
	
	/**
	 * 根据给出的时间，实例化时间工具对象
	 * @param timeMillis
	 * 		时间段 单位毫秒
	 */

	public ICalender(long timeMillis){
		setTimeMillis(timeMillis);
	}

	/**
	 * 根据传入的时间单位  得到对应的时间单位数
	 * @param timeType
	 * @return
	 */
	public int getTime(int timeType){
		long time = 0;
		switch(timeType){
		case YEAR:
			time = timeMillis / UNIT_YEAR;
			break;
		case MONTH:
			time = (timeMillis % UNIT_YEAR ) / UNIT_MONTH;
			break;
		case DAY:
			time = (timeMillis % UNIT_MONTH ) / UNIT_DAY;
			break;
		case HOUR:
			time = (timeMillis % UNIT_DAY ) / UNIT_HOUR;
			break;
		case MINUTE:
			time = (timeMillis % UNIT_HOUR ) / UNIT_MINUTE;
			break;
		case SECOND:
			time = (timeMillis % UNIT_MINUTE ) / UNIT_SECOND;
			break;
		}
		return (int)time;
	}
	
	
	/**
	 * 获取时间描述信息
	 * 	格式 :x年x月x日x时x分x秒
	 * @return
	 */
	public String getTimeString(){
		int type = UNIT_DAY | UNIT_HOUR | UNIT_MINUTE | UNIT_MONTH | UNIT_SECOND | UNIT_YEAR;
		return getTimeString(type);
	}
	/**
	 * 根据传入的时间类型 取到对应的时间描述
	 * @param timeType
	 * 			时间类型 可以用位或运算取到多个时间类型
	 * @return
	 */
	public String getTimeString(int timeType){
		StringBuffer sb = new StringBuffer();
		
		boolean isHave = false;
		if((timeType & UNIT_YEAR) != 0){
			int time = getTime(YEAR);
			if(time != 0){
				sb.append(time + "年");
				isHave = true;
			}
		}
		if((timeType & UNIT_MONTH) != 0){
			int time = getTime(MONTH);
			if(time != 0 || isHave){
				sb.append(time + "月");
				isHave = true;
			}
		}
		if((timeType & UNIT_DAY) != 0){
			int time = getTime(DAY);
			if(time != 0 || isHave){
				sb.append(time + "日");
				isHave = true;
			}
		}
		if((timeType & UNIT_HOUR) != 0){
			int time = getTime(HOUR);
			if(time != 0 || isHave){
				sb.append(time + "时");
				isHave = true;
			}
		}
		if((timeType & UNIT_MINUTE) != 0){
			int time = getTime(MINUTE);
			if(time != 0 || isHave){
				sb.append(time + "分");
				isHave = true;
			}
		}
		if((timeType & UNIT_SECOND) != 0){
			int time = getTime(SECOND);
			if(time != 0 || isHave){
				sb.append(time + "秒");
				isHave = true;
			}
		}
		return sb.toString();
	}
	
	public long getTimeMillis() {
		return timeMillis;
	}

	/**
	 * 设置时间参数
	 * @param timeMillis
	 * 		时间 单位毫秒
	 */
	public void setTimeMillis(long timeMillis) {
		this.timeMillis = timeMillis;
	}
	
}
